﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using NOSDC.Services;
using NOSDC.Models;
using NOSDC.Services.Extensions;

namespace NOSDC.Controllers
{
    public class EventsController : Controller
    {
        private readonly IEventService _service;

        #region ctors
        public EventsController(IEventService service)
        {
            _service = service;
        }

        public EventsController() : this(new EventService()) { }
        #endregion

        public ActionResult All()
        {
            var events = _service.GetAllEvents();
            return View(events);
        }

        public ActionResult Index(int? EventID, string Title)
        {
            var model = new SpecificEvent
            {
                Event = _service.GetEventByID(EventID ?? 0)
            };

            if (model.Event != null)
            {
                model.Event.EventAttendees = _service.GetAttendees(model.Event.EventID);
                if (Membership.GetUser() != null)
                    model.IsAttendingEvent = _service.IsAttendingEvent(model.Event.EventID, new Guid(Membership.GetUser().ProviderUserKey.ToString()));
            }

            return View(model);
        }

        public ActionResult UpcomingEvents()
        {
            var model = new EventsViewModel
                {
                    UpcomingEvents = _service.GetAllUpcomingEvents(),
                    PastEvents = _service.GetPreviousEvents(3),
                    TotalEvents = _service.GetTotalEvents()
                };
            
            return View(model);
        }

        /// <summary>
        /// Called when a user clicks attend on the right side panel
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Attend(int? eventId, bool main)
        {
            if (eventId != null)
            {
                var user = Membership.GetUser();
                if (user != null)
                {
                    _service.AttendEvent(new Guid(user.ProviderUserKey.ToString()), Convert.ToInt32(eventId));
                }
            }

            return main ? RedirectToAction("GetAttendees", new { eventId }) : RedirectToAction("GetUpcomingEvent");
        }

        public ActionResult GetAttendees(int? eventId)
        {
            if (eventId == null)
                return new EmptyResult();

            var attendees = _service.GetAttendees((int)eventId);
            var model = new EventsAttendeeViewModel();
            foreach (var e in attendees)
            {
                model.Attendees.Add(new EventsAttendeeModel()
                {
                    UserId = e.UserID,
                    Name = Membership.GetUser(e.UserID).UserName
                });
            }
                

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the event that is next to come.
        /// </summary>
        /// <returns></returns>
        public ActionResult GetUpcomingEvent()
        {
            var e = new UpcomingEvent
            {
                Event = _service.GetUpcomingEventWidget()
            };

            if (e.Event == null)
                e.Error = "Sorry, no upcoming events.";
            else
            {
                if (Membership.GetUser() != null)
                    e.IsAttendingUpcomingEvent = this._service.IsAttendingEvent(e.Event.EventID, new Guid(Membership.GetUser().ProviderUserKey.ToString()));
            }

            return PartialView("Events/_UpcomingEvent", e);
        }


        public ActionResult GetMoreEvents(int? page)
        {
            var cm = new MoreEventsViewModel();

            if (Request.IsAjaxRequest())
            {
                if (page != null)
                {
                    page++;
                    var events = _service.GetEventsByPage(page.Value, 3);
                    foreach(var e in events)
                    {
                        cm.Events.Add(new MoreEventsModel
                                     {
                                         Description = e.Description.StripHTML().Truncate(500),
                                         EventId = e.EventID,
                                         Icon = "/Images/Logos/" + e.Logo,
                                         Location = e.Location,
                                         StartDate = e.StartDate.ToString("MMMM  d, yyyy hh:mm"),
                                         Title = e.Title,
                                         Url = "/Events/" + e.EventID + "/" + e.Title.ToSeoUrl()
                                     });
                    }
                }
            }

            return Json(cm, JsonRequestBehavior.AllowGet);
        }
    }
}
